package com.explam.springboot.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.explam.springboot.common.ResultUtil;
import com.explam.springboot.mapper.PayMapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.explam.springboot.service.IPayService;
import com.explam.springboot.entity.Pay;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 支出表 前端控制器
 * </p>
 *
 * @author 作者
 * @since 2024-03-25
 */
@RestController
@RequestMapping("/admin/pay")
public class PayController {

    @Resource
    private IPayService payService;

    @Resource
    private PayMapper payMapper;

    /*
    * 获取一年内每个月的总支出
    * */
    @GetMapping("/month/total-monty")
    public ResultUtil getEveryMonthTotalMoneyPay(){
        List<Map<String, Object>> mapList = payMapper.getEveryMonthTotalMoneyPay();
        return ResultUtil.success(mapList);
    }

    /*
     * 计算支出总额
     * */
    @GetMapping("/total-money")
    public ResultUtil sumPayMoneyTotal(){
        Double payMoneyTotal = payMapper.payMoneyTotal();
        return ResultUtil.success("支出总金额", payMoneyTotal);
    }

    //修改
    @PostMapping("/update")
    public ResultUtil update(@RequestBody Pay pay){
        payService.saveOrUpdate(pay);
        return ResultUtil.success();
    }

    //新增
    @PostMapping("/save")
    public ResultUtil save(@RequestBody Pay pay){
        payService.save(pay);
        return ResultUtil.success();
    }

        //删除
    @GetMapping("/delete/id={id}")
    public ResultUtil delete(@PathVariable Integer id) {
        payService.removeById(id);
        return ResultUtil.success();
    }

    //批量删除
    @PostMapping("/del/batch")
    public ResultUtil deleteBatch(@RequestBody List<Integer> ids){
         payService.removeByIds(ids);
        return ResultUtil.success();
    }

        //查询所有数据
    @GetMapping("/all")
    public ResultUtil findAll() {
        return ResultUtil.success(payService.list());
    }

        //根据id查询
    @GetMapping("/get-pay/id={id}")
    public ResultUtil findOne(@PathVariable Integer id) {
        return ResultUtil.success(payService.getById(id));
    }

        //分页查询
    @GetMapping("/page")
    public ResultUtil findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String nickname,
                           @RequestParam(defaultValue = "") String title) {
        QueryWrapper<Pay> queryWrapper = new QueryWrapper<>();

        if (!"".equals(nickname)){
            queryWrapper.like("nick_name", nickname);
        }
        if (!"".equals(title)){
            queryWrapper.like("title", title);
        }
        queryWrapper.orderByDesc("id");
        return ResultUtil.success(payService.page(new Page<>(pageNum, pageSize),queryWrapper));
    }
}

